﻿using System;
using System.Windows.Forms;

namespace ColorWanted.Plugin.Base
{
    public abstract class PluginFormBase<FormType> : PluginBase where FormType : Form, new()
    {
        protected FormType form;

        public override void Open()
        {
            if (form != null && (form.Disposing || form.IsDisposed))
            {
                form = null;
            }
            if (form == null)
            {
                form = new FormType();
                form.FormClosed += Form_FormClosed;
            }
            form.Show();
            form.BringToFront();
        }

        private void Form_FormClosed(object sender, FormClosedEventArgs e)
        {
            Dispose();
        }

        public override void Dispose()
        {
            if (form == null)
            {
                return;
            }
            if (!(form.Disposing || form.IsDisposed))
            {
                form.Dispose();
            }
            form = null;
            GC.Collect();
        }
    }
}
